package cn.dankal.lequ.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dankal.lequ.R;
import cn.dankal.lequ.commom.util.LogUtil;
import cn.dankal.lequ.commom.util.ValidateUtil;
import cn.dankal.lequ.ui.customview.RegisterPoint;

public class ForgetPasswdActivity extends AppCompatActivity {
    boolean isChecked = false;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tv3)
    TextView tv3;

    @Bind(R.id.iv1)
    ImageView iv1;
    @Bind(R.id.iv2)
    ImageView iv2;
    @Bind(R.id.iv3)
    ImageView iv3;

    @Bind(R.id.point1)
    RegisterPoint point1;
    @Bind(R.id.point2)
    RegisterPoint point2;

    @Bind(R.id.v1)
    View v1;
    @Bind(R.id.v2)
    View v2;

    @Bind(R.id.et_phonenum)
    EditText et_phonenum;
    @Bind(R.id.et_validate)
    EditText et_validate;
    @Bind(R.id.et_passwd)
    EditText et_passwd;
    @Bind(R.id.tv_left_time)
    TextView tv_left_time;
    @Bind(R.id.autoll)
    LinearLayout autoll;

    @Bind(R.id.register_first)
    Button register_first;
    @Bind(R.id.register_second)
    Button register_second;

    @Bind(R.id.register_third)
    Button register_third;
    @Bind(R.id.iv_showpasswd)
    ImageView iv_showpasswd;
    @Bind(R.id.tv_title)
    TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_forgetpasswd);
        ButterKnife.bind(this);
        tv_title.setText("修改密码");
    }

    @OnClick({R.id.register_first, R.id.register_second, R.id.register_third, R.id.bt_back, R.id.iv_showpasswd, R.id.et_passwd})
    public void forgetClick(View view) {
        switch (view.getId()) {
            case R.id.register_first:
                String mobNumber = et_phonenum.getText().toString().trim();
                if (ValidateUtil.isContainSpace(et_phonenum.getText().toString()) || mobNumber.length() != 11)
                    LogUtil.showToast("号码输入不合法");
                else {
                    if (!checkNumisRegitered(mobNumber)) {
                        et_phonenum.setVisibility(View.INVISIBLE);
                        et_validate.setVisibility(View.VISIBLE);
                        autoll.setVisibility(View.VISIBLE);
                        tv1.setTextColor(Color.parseColor("#aaaaaa"));
                        tv2.setTextColor(Color.parseColor("#31c37c"));
                        point1.setColor(Color.parseColor("#31c37c"));
                        v1.setBackgroundColor(Color.parseColor("#31c37c"));
                        iv1.setVisibility(View.INVISIBLE);
                        iv2.setVisibility(View.VISIBLE);
                        tv_left_time.setText("60");
                        register_first.setVisibility(View.INVISIBLE);
                        register_second.setVisibility(View.VISIBLE);
                    } else
                        LogUtil.showToast("该号码已经注册过,请使用其他号码注册");

                }

                break;
            case R.id.register_second:
                String validate = et_validate.getText().toString().trim();
                et_validate.setVisibility(View.INVISIBLE);
                et_passwd.setVisibility(View.VISIBLE);

                iv2.setVisibility(View.INVISIBLE);
                tv2.setTextColor(Color.parseColor("#aaaaaa"));
                v2.setBackgroundColor(Color.parseColor("#31c37c"));
                iv3.setVisibility(View.VISIBLE);
                tv3.setTextColor(Color.parseColor("#31c37c"));

                point2.setColor(Color.parseColor("#31c37c"));
                register_second.setVisibility(View.INVISIBLE);

                autoll.setVisibility(View.INVISIBLE);
                iv_showpasswd.setVisibility(View.VISIBLE);
                register_third.setVisibility(View.VISIBLE);

                break;
            case R.id.register_third:
                if (ValidateUtil.isContainSpace(et_passwd.getText().toString()))
                    LogUtil.showToast("密码请不要包含空格");
                if (TextUtils.isEmpty(et_passwd.getText().toString()))
                    LogUtil.showToast("密码不为空");
                else {
                    //提交到服务器


                }
                break;
            case R.id.et_passwd:
                et_passwd.setCursorVisible(true);
                break;
            case R.id.bt_back:
                this.finish();
                break;
            case R.id.iv_showpasswd:
                if (isChecked) {
                    // 显示密码
                    et_passwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    iv_showpasswd.setImageResource(R.mipmap.ic_showpasswd);
                    isChecked = false;
                } else {
                    // 隐藏密码
                    et_passwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    isChecked = true;
                    iv_showpasswd.setImageResource(R.mipmap.ic_hidepasswd);

                }
                //保持光标永远在最后一个
                et_passwd.setSelection(et_passwd.getText().length());
                //当et_passwd被清空还原密码不可见性
                et_passwd.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        if (et_passwd.getText().length() == 0) {
                            et_passwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                            isChecked = true;
                            iv_showpasswd.setImageResource(R.mipmap.ic_hidepasswd);
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                    }
                });

                break;
            default:
                break;
        }
    }

    //服务器验证号码
    private boolean checkNumisRegitered(String mobNumber) {

//        if ()
//        return true;

        return false;
    }
}
